package ms.unicorn.erml.edit.policies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;

import ms.unicorn.diagram.edit.policies.UnicornBaseItemSemanticEditPolicy;
import ms.unicorn.erml.ERML;
import ms.unicorn.modellanguage.HintedSpecializationType;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageEntity;

public class SubPartCompartmentEditPolicy extends UnicornBaseItemSemanticEditPolicy {

	public SubPartCompartmentEditPolicy(IElementType elementType) {
		super(elementType);
	}
	
	public SubPartCompartmentEditPolicy() {
		this(ERML.SubPart.getElementType());
	}

	@Override
	protected Command getCreateCommand(CreateElementRequest req) {
		IElementType type = req.getElementType();
		if (type instanceof HintedSpecializationType && type != ERML.SubPart.getElementType()) {
			HintedSpecializationType hintType = (HintedSpecializationType) type;
			LanguageElement e = hintType.getElement();
			if (e instanceof LanguageEntity) {
				ICommand creationCommand =  ((LanguageEntity)e).getEditHelperAdvice().getCreationCommand(hintType.getElement(), req);
				if (creationCommand != null)
					return getGEFWrapper(creationCommand);
			}
		}
		return super.getCreateCommand(req);
	}
}
